//
//  ICContactListCell.m
//  ICome
//
//  Created by zhang_rongwu on 16/3/7.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICContactListCell.h"
#import "UIImage+AvatarGeneration.h"

#define headImageHeight (36 * Level)
#define margin 10

@interface ICContactListCell ()

@property (nonatomic, strong) UILabel *masterLabel;
@property (nonatomic, strong) UILabel *companyNameLabel;
@property (nonatomic, strong) UILabel *oNameLabel;


@end

@implementation ICContactListCell
- (id)initWithStyle:(UITableViewCellStyle)cell reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:cell reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftFreeSpace = 15;
        [self addSubview:self.selectBtn];
    }
    return self;
}

-(void)setItem:(ICContactItem *)item {
    if ([item.icon hasPrefix:@"icon"]) {
        self.headImageView.image = [UIImage imageNamedFromiComeSDKBundle:item.icon];
    } else {
        [UIImage addAvatariamgeView:self.headImageView withUrlStr:item.icon witheId:item.eId withName:item.title];
    }
    self.titleLabel.text = item.title;
}

- (void)setIsMaster:(BOOL)isMaster
{
    if (isMaster) {
        self.masterLabel.text = @"管理员";
    } else {
        self.masterLabel.text = @" ";
    }
    
}

- (void)setSubTitleModel:(ICEmpInfoModel *)subTitleModel {
    self.companyNameLabel.text = subTitleModel.companyName;
    self.oNameLabel.text = subTitleModel.oName;
    if (subTitleModel.companyName.length > 0) {
        self.oNameLabel.text = [NSString stringWithFormat:@" / %@", subTitleModel.oName];
    }
    [self subTitleWithCompanyName:subTitleModel.companyName oName:self.oNameLabel.text width:App_Frame_Width - margin*3 - headImageHeight];
    if (subTitleModel.isOrg) {
        self.oNameLabel.text = @"";
    }
}


- (void)subTitleWithCompanyName:(NSString *)companyName oName:(NSString *)oName width:(CGFloat)width {
    CGFloat cWidth = [companyName sizeForFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width;
    CGFloat oWidth = [oName sizeForFont:[UIFont systemFontOfSize:14] size:CGSizeMake(MAXFLOAT,MAXFLOAT) mode:NSLineBreakByWordWrapping].width+1;
    if ((cWidth + oWidth) > width) {
        if (cWidth > oWidth) {
            [self.oNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom);
                make.right.equalTo(self.titleLabel.mas_right);
                make.width.mas_equalTo(oWidth);
                make.height.mas_equalTo(23);
            }];
            [self.companyNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom);
                make.left.equalTo(self.titleLabel.mas_left);
                make.width.mas_equalTo(width - oWidth);
                make.height.mas_equalTo(23);
            }];
        } else {
            [self.companyNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom);
                make.left.equalTo(self.titleLabel.mas_left);
                make.width.mas_equalTo(cWidth);
                make.height.mas_equalTo(23);
            }];
            [self.oNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom);
                make.right.equalTo(self.titleLabel.mas_right);
                make.width.mas_equalTo(width - cWidth);
                make.height.mas_equalTo(23);
            }];
        }
    } else {
        [self.companyNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.left.equalTo(self.titleLabel.mas_left);
            make.width.mas_equalTo(cWidth);
            make.height.mas_equalTo(23);
        }];
        [self.oNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.right.equalTo(self.titleLabel.mas_right);
            make.width.mas_equalTo(width - cWidth);
            make.height.mas_equalTo(23);
        }];
    }
}

- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headImageHeight, headImageHeight)];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = 5;
        [self addSubview:_headImageView];
    }
    return _headImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = ICTEXTCOLOR_T1;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.numberOfLines = 0;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)companyNameLabel {
    if (!_companyNameLabel) {
        _companyNameLabel = [[UILabel alloc] init];
        _companyNameLabel.textColor = ICRGB(0x999999);
        _companyNameLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_companyNameLabel];
    }
    return _companyNameLabel;
}

- (UILabel *)oNameLabel {
    if (!_oNameLabel) {
        _oNameLabel = [[UILabel alloc] init];
        _oNameLabel.textColor = ICRGB(0x999999);
        _oNameLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_oNameLabel];
    }
    return _oNameLabel;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.centerY.equalTo(self.mas_centerY);
        make.height.and.width.mas_equalTo(headImageHeight);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(16);
        make.left.equalTo(self.headImageView.mas_right).offset(margin);
        make.right.equalTo(self.mas_right).offset(-margin);
        make.height.mas_equalTo(23);
    }];
    
    [self.masterLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.titleLabel.mas_right).offset(margin);
    }];
//    CAShapeLayer *layer = [CAShapeLayer layer];
//    UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:_headImageView.bounds];
//    layer.path = apath.CGPath;
//    _headImageView.layer.mask = layer;
    
    [self.selectBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-15);
    }];
}

- (UILabel *)masterLabel {
    if (!_masterLabel) {
        _masterLabel = [[UILabel alloc] init];
        _masterLabel.font = ICFont(12 * Level);
        [self.contentView addSubview:_masterLabel];
    }
    return _masterLabel;
}

-(UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [[UIButton alloc] init];
        [_selectBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_email_unchecked"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_email_checked"] forState:UIControlStateHighlighted];
        [_selectBtn addTarget:self action:@selector(seltctBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _selectBtn.hidden = YES;
    }
    return _selectBtn;
}

- (void)seltctBtnClick:(UIButton *)sender {
    if (self.select) {
        self.select(self);
    }
}
@end

